app.controller('RegionBfInfoCtrl', ['$scope','$state', '$stateParams','$http','DiagramService','$anchorScroll','$location','DataService',RegionBfInfoCtrl]);

function RegionBfInfoCtrl($scope,$state, $stateParams,$http,DiagramService,$anchorScroll,$location,DataService){
	$anchorScroll.yOffset = -40; 
	$location.hash();
	$anchorScroll();

	$scope.print = function(){
		
	}

	var maxGroup = [
		{name:'110警情',max:30},
		{name:'打防控案情',max:30},
		{name:'网吧旅馆触网',max:30},
		{name:'黄赌毒举报',max:30},
		{name:'流动人口数',max:30},
		{name:'边缘性场所',max:30}
	]

	$scope.radar = {
		'indicator':maxGroup,
		'data':[],
		'name':'得分详情'
	}


	$scope.openXq =function(sfzh){
		console.log(sfzh)
	  var url = $state.href('app.netbar.info',{id:sfzh});
	  window.open(url,'_blank');
	}

  var yesterday = new Date('2017-08-13');
  // yesterday.setDate(yesterday.getDate() - 1);
  var last7 = new Date('2017-08-06');
  // last7.setDate(last7.getDate() - 8);
  var last30 = new Date('2017-07-14');
  // last30.setDate(last30.getDate() - 30);
  $scope.id = $stateParams.id;
  $scope.name = $stateParams.name;
	$scope.bfInfo = {
		id:$scope.id,
		name:$scope.name,
		jd:'',
		rks:0,
		dj:0,
		df:0
	};


	$scope.goBack=function(){
		history.back()
	}

	//显示各组的得分信息
	$scope.dfGroup= {
		'110警情':{
			ms:'警情数很多，人均警情比偏移值高25%，七日均值超三十日均值偏移值24%。',
			df:0
		},
		'打防控案情':{
			ms:'落地侵财案情数很多，人均警情比偏移值高25%，七日均值超三十日均值偏移值22%。',
			df:0
		},
		'网吧旅馆触网':{
			ms:'七类重点人员底数千人比均值超全局均值13%，区域七日旅馆、网吧人数超12人。',
			df:0
		},
		'黄赌毒举报':{
			ms:'30日内发生了3次黄赌毒举报。',
			df:0
		},
		'流动人口数':{
			ms:'流动人口数：',
			df:0
		},
		'边缘性场所':{
			ms:'边缘性场所数:',
			df:0
		}
	};

	//历史得分纪录
	DataService.restGetApi('area_totals?filter={"where":{"community": "' + $scope.name + '"}, "order": "date"}')
    .then(function success (data) {
      $scope.historyDf = data;
    });
  //社区信息
	DataService.restGetApi('communities?filter={"where":{"community": "' + $scope.name + '"}}')
    .then(function success (data) {
      var item = data[0];
      $scope.bfInfo.jd = item.street;
      $scope.bfInfo.rks = item.mark;
    });
  //社区得分情况
  DataService.restGetApi('area_totals?filter={"where":{"date":"' + yesterday.format('yyyy-MM-dd') + '", "community": "' + $scope.name + '"}}')
    .then(function success (data) {
      var item = data[0];
      $scope.dfGroup['110警情'].df = item.jq_score;
      $scope.dfGroup['打防控案情'].df = item.dfk_score;
      $scope.dfGroup['网吧旅馆触网'].df = item.net_score;
      $scope.dfGroup['黄赌毒举报'].df = item.hdd_score;
      $scope.dfGroup['流动人口数'].df = item.flow_score;
      $scope.dfGroup['边缘性场所'].df = item.enter_score;

      //填入数据
      for(var i = 0;i<maxGroup.length;i++){
        $scope.radar.data.push($scope.dfGroup[maxGroup[i].name].df)
      }
    });
  //社区各项数据情况
  DataService.restGetApi('area_items?filter={"where":{"date":"' + yesterday.format('yyyy-MM-dd') + '", "community": "' + $scope.name + '"}}')
    .then(function success (data) {
      var item = data[0];
      $scope.dfGroup['110警情'].ms = '人均警情比偏移值高' + (item.jq_7_percent * 100) + '%, 七日均值超三十日均值偏移值' + (item.jq_30_percent * 100) + '%';
      $scope.dfGroup['打防控案情'].ms = '人均案件比偏移值高' + (item.dfk_7_percent * 100) + '%, 七日均值超三十日均值偏移值' + (item.dfk_30_percent * 100) + '%';
      $scope.dfGroup['网吧旅馆触网'].ms = '区域七日旅馆、网吧触网人次：' + item.net_num;
      $scope.dfGroup['黄赌毒举报'].ms = '30日内黄赌毒举报次数：' + item.hdd_num;
      $scope.dfGroup['流动人口数'].ms = '流动人口数：' + item.flow_num;
      $scope.dfGroup['边缘性场所'].ms = '边缘性场所数：' + item.enter_num;

    });
	
	$scope.radarConfig = {
    theme:'default',
    dataLoaded:true
  };
  $scope.radarOption = DiagramService.getRadar($scope.radar)
  $scope.lineConfig = {
      theme:'default',
      dataLoaded:false
  };
  //警情
  DataService.restGetApi('police_communities?filter={"where":{"date":{"gt": "' + last7.format('yyyy-MM-dd') + '"}, "community": "' + $scope.name + '"}, "order": "bjsj desc"}')
    .then(function success (data) {
      $scope.jingqings = data;
    });
  var line = {
  	title:'三十日区域人均警情偏离曲线图',
  	legend:['三十日区域人均偏离'],
  	xAxis:[],
  	data:[[]]
  }
  // $scope.lineOption = DiagramService.getLine(line.title,line.legend,line.xAxis,line.data)
  var line2 = {
  	title:'七日区域警情均值偏离曲线图',
  	legend:['七日警情均值偏离'],
  	xAxis:[],
  	data:[[]]
  }
  // $scope.lineOption2 = DiagramService.getLine(line2.title,line2.legend,line2.xAxis,line2.data)

  //偏离值
  DataService.restGetApi('area_items?filter={"where":{"date":{"gt": "' + last7.format('yyyy-MM-dd') + '"}, "community": "' + $scope.name + '"}, "order": "date"}')
    .then(function success (data) {
      var xAxis = [];
      for(var i = 0; i < data.length; i++) {
        var dates = data[i].date.substr(5);
        xAxis.push(dates);
        line.data[0].push(data[i].jq_7_percent);
        line2.data[0].push(data[i].jq_30_percent);
        line3.data[0].push(data[i].dfk_7_percent);
        line4.data[0].push(data[i].dfk_30_percent);
        line5.data[0].push(data[i].net_num);
        // bar1.data[0].push(data[i].net_num);
      }
      line.xAxis = xAxis;
      line2.xAxis = xAxis;
      line3.xAxis = xAxis;
      line4.xAxis = xAxis;
      line5.xAxis = xAxis;
      bar1.xAxis = xAxis;
      $scope.lineOption = DiagramService.getLine(line.title,line.legend,line.xAxis,line.data);
      $scope.lineOption2 = DiagramService.getLine(line2.title,line2.legend,line2.xAxis,line2.data);
      $scope.lineOption3 = DiagramService.getLine(line3.title,line3.legend,line3.xAxis,line3.data);
      $scope.lineOption4 = DiagramService.getLine(line4.title,line4.legend,line4.xAxis,line4.data);
      $scope.lineOption5 = DiagramService.getLine(line5.title,line5.legend,line5.xAxis,line5.data)
      $scope.barOption1 = DiagramService.getStackBar(bar1.title,bar1.legend,bar1.xAxis,bar1.data)
      $scope.lineConfig.dataLoaded= true;
    });

  //案情
  DataService.restGetApi('case_dfks/getInfo?community=' + $scope.name + '&date=' + last7.format('yyyy-MM-dd'))
   .then(function success (data) {
     $scope.anqings = data;
   });
  var line3 = {
  	title:'三十区域人均案情偏离曲线图',
  	legend:['三十日区域人均偏离'],
  	xAxis:[],
  	data:[[]]
  }
  console.log(line3.data)
 /* $scope.lineOption3 = DiagramService.getLine(line3.title,line3.legend,line3.xAxis,line3.data)
*/
  var line4 = {
  	title:'七日区域案情均值偏离曲线图',
  	legend:['七日案情均值偏离'],
  	xAxis:[],
  	data:[[]]
  }
  // $scope.lineOption4 = DiagramService.getLine(line4.title,line4.legend,line4.xAxis,line4.data)
 
  //重点人员
  var line5 = {
  	title:'区域七日旅馆、网吧触网总人次曲线图',
  	legend:['七日触网人次'],
  	xAxis:[],
  	data:[[],[]]
  }
  // $scope.lineOption5 = DiagramService.getLine(line5.title,line5.legend,line5.xAxis,line5.data)

  var bar1 = {
  	title:'区域七日旅馆、网吧触网人数柱状图',
  	legend:['触网人数'],
  	xAxis:[],
  	data:[[]]
  }
  // $scope.barOption1 = DiagramService.getStackBar(bar1.title,bar1.legend,bar1.xAxis,bar1.data)

  DataService.restPostApi('entersites/getData', {community: $scope.name})
    .then(function success (data) {
      $scope.zdry = data;
    });

  //黄赌毒案件
  DataService.restGetApi('pornographies?filter={"where":{"time":{"gt": "' + last30.format('yyyy-MM-dd') + '"}, "community": "' + $scope.name + '"}, "order": "time desc"}')
    .then(function success (data) {
      $scope.hddcase = data;
    });

  DataService.restGetApi('entersites?filter={"where": {"community": "' + $scope.name + '"}}')
    .then(function success (data) {
      $scope.byPlace = data;
    });


  $scope.flowPeople = [
		{
			time:'2017年6月18日',
			amount:'22.5万',
			change:'1000',
			df:'1'
		},
		{
			time:'2017年6月25日',
			amount:'22.5万',
			change:'-2000',
			df:'0'
		},
		{
			time:'2017年7月2日',
			amount:'22.5万',
			change:'-2000',
			df:'0'
		},
		{
			time:'2017年7月9日',
			amount:'22.5万',
			change:'-2000',
			df:'0'
		},
		{
			time:'2017年7月16日',
			amount:'22.5万',
			change:'-2000',
			df:'0'
		},
		{
			time:'2017年7月23日',
			amount:'22.5万',
			change:'-2000',
			df:'0'
		}
		,{
			time:'2017年7月30日',
			amount:'22.5万',
			change:'-2000',
			df:'0'
		}
		,{
			time:'2017年8月6日',
			amount:'22.5万',
			change:'-2000',
			df:'0'
		}
		,{
			time:'2017年8月13日',
			amount:'22.8万',
			change:'-2000',
			df:'0'
		}
	]

	function Date2MonthWeek(day){
		var y = day.getYear();
		var m = day.getMonth()+1;
		var d = day.getDate();
		var date = new Date(y, parseInt(m) - 1, d), w = date.getDay(), dd = date.getDate(); 
		var weekNum = Math.ceil( (dd + 6 - w) / 7); 
		var yy = day.getFullYear();
		return yy+'年'+m+'月第'+weekNum+'周';
	};

	var date = new Date();
	$scope.weekChoice = Date2MonthWeek(date);

/*	

	//用于控制顶部选项是否选中
	$scope.ctrlOption = []
	//用于控制顶部选项是否存在
	$scope.checkOption = []

	$scope.haveInfo=function(val){
		if($scope.ctrlOption.indexOf(val)>-1){
			return true;
		}else{
			return false;
		}
	}

	$scope.checkInfo=function(val){
		var index = $scope.ctrlOption.indexOf(val);
		if(index>-1){
			$scope.ctrlOption.splice(index,1)
		}else{
			$scope.ctrlOption.push(val)
		}
	}

	$scope.getSuggestion = function(){
		$scope.editState = 'noedit'
		$scope.choiceSuggest = ''
		$scope.suggest_save = $scope.choiceSuggest;
		$scope.suggestion='';
		$scope.suggestion_save = $scope.suggestion;
		if($scope.choiceSuggest==''&&$scope.suggestion==''){
			$scope.editState = 'edit'
		}
	}

	$scope.getSuggestion();

	$scope.openEdit = function(){
		$scope.editState='edit'
	}

	$scope.resetSuggestion = function(){
		$scope.editState = 'onlyread'
		$scope.choiceSuggest = $scope.suggest_save;
		$scope.suggestion = $scope.suggestion_save;
	}

	$scope.submitSuggestion = function(){
		$scope.editState = 'onlyread'
		$scope.suggest_save = $scope.choiceSuggest;
		$scope.suggestion_save = $scope.suggestion;
	}

	$scope.chooseSuggest = function(index){
		if($scope.editState=='edit'){
			$scope.choiceSuggest = index;
		}
	}

	$scope.emptyTextArea = function(){
		$scope.suggestion = '';
	}*/

}
